package mmap

import (
	"log"
	"os"
	"syscall"
)

func mmap() {
	file, err := os.OpenFile("mmap.bin", os.O_RDWR|os.O_CREATE, 0644)
	if err != nil {
		log.Fatalln(err)
	}

	if _, err := file.WriteAt([]byte{0}, 1<<9); err != nil {
		log.Fatalln(err)
	}

	data, err := syscall.Mmap(int(file.Fd()), 0, 1<<8, syscall.PROT_WRITE, syscall.MAP_SHARED)

	if err != nil {
		log.Fatalln(err)
	}

	if err = file.Close(); err != nil {
		log.Fatalln(err)
	}

	for i, v := range []byte("aaaello, MMap") {
		data[i] = v
	}

	data[254] = byte('B')

	if err = syscall.Munmap(data); err != nil {
		log.Fatalln(err)
	}

}
